﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Linq.Expressions;
using Sisocana.Core;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Dal.EntityModels;
using Sisocana.Dal;
using Sisocana.Dal.Interfaces;
using System.Data;


namespace Sisocana.Core
{
    public class VideoAsociadoService : ServiceBase<IVideoAsociadoModel>, IVideoAsociadoService
    {
        public VideoAsociadoService()
            : this(new VideoAsociadoRepository())
        {
        }

        private IVideoAsociadoRepository _videoRepository;

        public VideoAsociadoService(IVideoAsociadoRepository videoRepository)
        {
            _videoRepository = videoRepository ?? new VideoAsociadoRepository();
        }

        public void createVideoAsociadoMapperEF2Model()
        {
            AutoMapper.Mapper.CreateMap<VideoAsociado, VideoAsociadoModel>()
                .ForMember(s => s.apiKey, opt => opt.MapFrom(c => c.apiKey.Trim()))
                .ForMember(s => s.tagBusqueda, opt => opt.MapFrom(c => c.tagBusqueda.Trim()))
                .ForMember(s => s.VideoAsociadoName, opt => opt.MapFrom(c => c.Video.nombre.Trim()))
                .ForMember(s => s.idSitio, opt => opt.MapFrom(c => c.Sitio.id));
        }

        public void createVideoAsociadoMapperModel2EF()
        {
            AutoMapper.Mapper.CreateMap<VideoAsociadoModel, VideoAsociado>()
                .ForMember(s => s.Sitio.id, opt => opt.MapFrom(c => c.idSitio));
        }

        public int createUpdateVideoAsociado(VideoAsociadoModel vam)
        {
            //busco la asociacion 
            VideoAsociado video = _videoRepository.GetSingle(c => c.Sitio.id == vam.idSitio);
            if (video != null) 
            {
                video.apiKey = vam.apiKey;
                video.cantVisualizar = vam.cantVisualizar;
                video.tagBusqueda = vam.tagBusqueda;
                video.tipo = vam.tipo;
                video.width = vam.width;
                video.heigth = vam.heigth;

                try
                {
                    _videoRepository.Save();
                    return 0;
                }
                catch (Exception e)
                {
                    return e.InnerException.GetHashCode();
                }            
            }

            else
            {
                VideoAsociado viEF = new VideoAsociado();
                viEF.apiKey = vam.apiKey;
                viEF.cantVisualizar = vam.cantVisualizar;
                viEF.tagBusqueda = vam.tagBusqueda;
                viEF.tipo = vam.tipo;
                viEF.width = vam.width;
                viEF.heigth = vam.heigth;

                FuenteRepository _fuenteRepository = new FuenteRepository();
                IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
                IEnumerable<Video> listrssEF = fuentesEF.OfType<Video>();

                //Siempre selecciona un nombre desde la interfaz
                Video videoEF = listrssEF.AsQueryable<Video>().Where(c => c.nombre.Trim() == vam.VideoAsociadoName.Trim()).First();

                SiteRepository _SiteRepository = new SiteRepository();
                Sitio sitEF = _SiteRepository.GetSingle(c => c.id == vam.idSitio);

                viEF.Sitio = sitEF;
                viEF.Video = videoEF;

                _videoRepository.Add(viEF);

                try
                {
                    _videoRepository.Save();
                    return 0;
                }
                catch (Exception e)
                {
                    return e.InnerException.GetHashCode();
                }
            }
        }

        public VideoAsociadoModel GetSingle(Expression<Func<VideoAsociadoModel, bool>> whereCondition)
        {


            createVideoAsociadoMapperEF2Model();

            IList<VideoAsociado> vaEF = _videoRepository.GetAll();
            List<VideoAsociadoModel> va = new List<VideoAsociadoModel>();

            va = AutoMapper.Mapper.Map(vaEF, va);

            IQueryable<VideoAsociadoModel> vaRet = va.AsQueryable<VideoAsociadoModel>().Where(whereCondition);

            List<VideoAsociadoModel> ret = vaRet.ToList();

            if (ret.Count == 0)
                return null;

            return ret.First();
        }
        public void Add(VideoAsociadoModel entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(VideoAsociadoModel entity)
        {
            throw new NotImplementedException();
        }

        public void Update(VideoAsociadoModel entity)
        {
            throw new NotImplementedException();
        }

        public IList<VideoAsociadoModel> GetAll(Expression<Func<VideoAsociadoModel, bool>> whereCondition)
        {
            createVideoAsociadoMapperEF2Model();

            IList<VideoAsociado> vaEF = _videoRepository.GetAll();
            List<VideoAsociadoModel> va = new List<VideoAsociadoModel>();
            va = AutoMapper.Mapper.Map(vaEF, va);
            IQueryable<VideoAsociadoModel> vaRet = va.AsQueryable<VideoAsociadoModel>().Where(whereCondition);
            return vaRet.ToList();

        }

        IList<VideoAsociadoModel> IService<VideoAsociadoModel>.GetAll()
        {
            throw new NotImplementedException();
        }

        public IQueryable<VideoAsociadoModel> Query(Expression<Func<VideoAsociadoModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count(Expression<Func<VideoAsociadoModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }
    }
}
